﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ListenMusic.Models.ViewModel;
using ListenMusic.Models.Reposistory.Public;
using System.ServiceModel;
using ListenMusic.Song_ServiceReference;
namespace ListenMusic.Controllers.Public
{
    public class ListenController : ApplicationController
    {
        //
        // GET: /Listen/          

        CommentReposistory _comment;
        public ListenController()
        {
            _comment = new CommentReposistory();
        }
        public ActionResult ListenPage(int pageindex,int songid)
        {            
            ListenPage listenpage;
            int pagesize = 5;
            double total = _comment.totalCommentby_SongID(songid) / pagesize;
            ViewBag.pageNumber = Math.Ceiling(total) + 1;
            Song s = _song.getSong_byID(songid);
            double duration = ((_listen.durationMP3(Server.MapPath("~/Music/" + s.SongURL))) - 60) * 1000;
            double setIterval = 0;
            if (duration > 10000)
            {
                setIterval = duration;
            }
            else
            {
                setIterval = 99999999999;
            }
            ViewBag.durationMP3 = setIterval;
            List<Song_ServiceReference.Album_Song> sas = _song.getSonginAlbum(songid);
            ViewBag.songid = songid;
            if (sas.Count == 0)
            {
                listenpage = _listen.loadListenPage_noAlbum(pageindex, pagesize,songid);    
                
                return View(listenpage);
            }
            else
            {
                listenpage = _listen.loadListenPage(pageindex, pagesize, songid);                  
                return View(listenpage);
            }
        }
        public ActionResult ListenAlbumPage(int albumid)
        {
            try
            {
                var files = _dowload.GetFiles();
                // ViewBag.album = _album.getAlbum_byID(albumid);
                int pagesize = 5;
                double total = _comment.totalCommentBy_AlbumID(albumid) / pagesize;
                ViewBag.pageNumber = Math.Ceiling(total) + 1;
                ViewBag.songAlbum = _song.getSong_byAlbumID(albumid);
                ViewBag.randomAlbum = _album.get8Random_Album();
                if (Session["user"] != null)
                {
                    string array_info_user = (string)Session["user"];
                    ViewBag.playList = _playlist.getPlaylist_ByMemberID(array_info_user);
                    ViewBag.songInPlaylist = _playlist.getSongInPlaylist(array_info_user);
                }
                return View("ListenAlbumPage");
            }
            catch (FaultException ex)
            {
                return View("~/Views/Shared/_Error.cshtml");
                throw ex;
            }
        }
        public ActionResult ViewVideos(int pageindex, int videoid)
        {
            if (pageindex < 1)
            {
                pageindex = 1;
            }
            ViewVideoPage viewvideopage;
            int pagesize = 10;
            int start = (pageindex -1)*pagesize;
            int end = pageindex*pagesize;
            Song s = _song.getSong_byID(videoid);
            ViewBag.durationVideo = ((_listen.durationMP3(Server.MapPath("~/Music/Videos/" + s.SongURL))) - 60) * 1000;
            viewvideopage = _listen.loadVideoPage(start, end, videoid);
            if (viewvideopage != null)
            {
                return View(viewvideopage);
            }
            else
            {
                return RedirectToAction("NotFound", "Song");
            }
            
        }
        public JsonResult updateCountView(int songid)
        {            
            try {
                bool rs = _song.updateCountView(songid);
                return Json(rs, JsonRequestBehavior.AllowGet);
            }
            catch (FaultException ex) { throw ex; }
        }
        public JsonResult updateCountView_Album(int albumid)
        {
            try
            {
                bool rs = _album.updateCountView(albumid);
                return Json(rs, JsonRequestBehavior.AllowGet);
            }
            catch (FaultException ex) { throw ex; }
        }
    }
}
